'''
* This is the projet for Brtc LlmOps Platform
* @Author Leon-liao <liaosiliang@alltman.com>
* @Description //TODO 
* @File: stu_code.py
* @Time: 2025/9/24
* @All Rights Reserve By Brtc
'''
import  dotenv
import weaviate
from langchain.retrievers import ParentDocumentRetriever
from langchain.storage import LocalFileStore
from langchain_community.document_loaders import UnstructuredFileLoader
from langchain_openai import OpenAIEmbeddings
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain_weaviate import WeaviateVectorStore
from weaviate.auth import AuthApiKey
#from langchain_unstructured import UnstructuredLoader
import os
dotenv.load_dotenv()
# 创建加载器与文档列表，并加载文档

loaders = [
    UnstructuredFileLoader("./project_api.md"),
    UnstructuredFileLoader("./eshop_goods.txt")

]
docs = []
for loader in loaders:
    docs.extend(loader.load())
# 创建文本解析器
text_splitter = RecursiveCharacterTextSplitter(chunk_size=500,chunk_overlap=50)
client = weaviate.connect_to_weaviate_cloud(
    skip_init_checks=True,
    cluster_url=os.getenv("WAEVIATE_URL"),
    auth_credentials=AuthApiKey(os.getenv("WEAVIATE_KEY"))
)
vector_store = WeaviateVectorStore(
    client=client,
    index_name="TestParent",
    text_key="text",
    embedding = OpenAIEmbeddings(model="text-embedding-3-small")
)

store = LocalFileStore("./parent-document")

# 创建父文档检索器
retriever = ParentDocumentRetriever(
    vectorstore=vector_store,
    byte_store = store,
    child_splitter=text_splitter
)

# # 添加文档
retriever.add_documents(docs)

#检索并返回
search_docs =retriever.invoke("分享关于LLMops 的一些应用配置")
print(search_docs)
client.close()